clear
set more off
capture log close
clear matrix

  *child ages to observe their adult incomes
  gl agemin=25 // youngest possible=25
  gl agemax=48 // oldest possible=48
  
  *child birth cohorts to use
  loc cohortmin=1952 // earliest possible=1952
  loc cohortmax=1993 // latest possible=1993
  
  
log using ${us_results}/irps_causal_vs_me_US.log, replace


  
*USE the ranked MAIN SAMPLE 
use *newid *LAB* AGE *cohort *LABYR* *MF* *empl* *schmax year female if year>=1985 using ${projdata}/analysis-sample-main.dta, clear

gen AGEC1=year-cohort-40 
gen f_LABAGE_1=f_LABYR-f_cohort 
gen m_LABAGE_1=m_LABYR-m_cohort 

forval i=2/4 {
	gen AGEC`i'=AGEC1^`i'
	gen f_LABAGE_`i'=f_LABAGE_1^`i'
	gen m_LABAGE_`i'=m_LABAGE_1^`i'
}
   
  * MOTHER's SAMPLE
 keep if pm_LAB!=. & m_LABAGE_1!=.
 keep if m_schmax!=. 
 keep if m_emplavg!=. 
 
* FATHER's SAMPLE
 keep if pf_LAB!=. & f_LABAGE_1!=.
 keep if f_schmax!=. 
 keep if f_emplavg!=. 
 

************************
* MOTHERS 

**** Early period   
preserve

loc yrmin=1985
loc yrmax=1995

* Subsample 
 keep if inrange(year,`yrmin',`yrmax') & inrange(AGE,${agemin},${agemax})
 
*1 IRP
 regress pLAB pm_LAB m_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp =_b[pm_LAB]
 matrix se =_se[pm_LAB]
 matrix R =e(r2)
 matrix N =e(N)
 
*2 IRP conditional on mother's education
 regress pLAB pm_LAB m_schmax m_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pm_LAB]
 matrix se= se, _se[pm_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)

*3 IRP conditional on mother's education and father's income and education
 regress pLAB pm_LAB m_schmax pf_LAB f_schmax m_LABAGE_? f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pm_LAB]
 matrix se= se, _se[pm_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
restore 

**** Late period  
preserve

loc yrmin=2008
loc yrmax=2018

* Subsample 
 keep if inrange(year,`yrmin',`yrmax') & inrange(AGE,${agemin},${agemax})
 
*4 IRP
 regress pLAB pm_LAB m_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pm_LAB]
 matrix se= se, _se[pm_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
*5 IRP conditional on mother's education
 regress pLAB pm_LAB m_schmax m_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pm_LAB]
 matrix se= se, _se[pm_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)

*6 IRP conditional on mother's education and father's income and education
 regress pLAB pm_LAB m_schmax pf_LAB f_schmax m_LABAGE_? f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pm_LAB]
 matrix se= se, _se[pm_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
restore  




************************
* FATHERS 

***** Early period  
preserve

loc yrmin=1985
loc yrmax=1995

* Subsample 
 keep if inrange(year,`yrmin',`yrmax') & inrange(AGE,${agemin},${agemax})
 
*7 IRP
 regress pLAB pf_LAB f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pf_LAB]
 matrix se= se, _se[pf_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
*8 IRP conditional on father's education
 regress pLAB pf_LAB f_schmax f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pf_LAB]
 matrix se= se, _se[pf_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)

 
*9 IRP conditional on father's education and mother's income and education
 regress pLAB pf_LAB f_schmax pm_LAB m_schmax m_LABAGE_? f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pf_LAB]
 matrix se= se, _se[pf_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
restore 

***** Late period for fathers 
preserve

loc yrmin=2008
loc yrmax=2018

* Subsample 
 keep if inrange(year,`yrmin',`yrmax') & inrange(AGE,${agemin},${agemax})
 
*10 IRP
 regress pLAB pf_LAB f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pf_LAB]
 matrix se= se, _se[pf_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
*11 IRP conditional on father's education
 regress pLAB pf_LAB f_schmax f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pf_LAB]
 matrix se= se, _se[pf_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)

*12 IRP conditional on father's education and mother's income and education
 regress pLAB pf_LAB f_schmax pm_LAB m_schmax m_LABAGE_? f_LABAGE_? AGEC? i.year , cluster(newid)
 matrix irp= irp, _b[pf_LAB]
 matrix se= se, _se[pf_LAB]
 matrix R= R, e(r2)
 matrix N= N, e(N)
 
restore  
 
 


**** Save results to datset 

clear 
set obs 1
gen temp=1 
 svmat irp
 svmat se
 svmat R 
 svmat N 
 reshape long irp se R N, i(temp) j(regnum) 
 drop temp 

gen mother=. 	// mother IRP?
replace mother=1 if inrange(regnum,1,6)
replace mother=0 if inrange(regnum,7,12)

gen period=.	// time period
replace period=1 if inlist(regnum,1,2,3,7,8,9)
replace period=2 if inlist(regnum,4,5,6,10,11,12)

gen specif=.	// specification (3 for each parent/period)
replace specif=1 if inlist(regnum,1,4,7,10)
replace specif=2 if inlist(regnum,2,5,8,11)
replace specif=3 if inlist(regnum,3,6,9,12)

label define regnum_vals ///
1 "IRP" ///
2 "IRP conditional on mother's education" ///
3 "IRP conditional on mother's education and father's income and education" ///
4 "IRP" ///
5 "IRP conditional on mother's education" ///
6 "IRP conditional on mother's education and father's income and education" ///
7 "IRP" ///
8 "IRP conditional on father's education" ///
9 "IRP conditional on father's education and mother's income and education" ///
10 "IRP" ///
11 "IRP conditional on father's education" ///
12 "IRP conditional on father's education and mother's income and education"

label values regnum regnum_vals 
order regnum mother period specif, first 
 
qui compress
save ${us_results}/US_irps.dta, replace



clear
log close 

